from UserInterface.MyLogger import *
import logging

class DefaultLogger(MyLogger):
    def __init__(self, level:LogLevel):
        super().__init__(level)
        self.logger = logging.getLogger('default')
        defaultFormat = '[%(asctime)s] [%(levelname)s]: %(message)s'
        self.defaultFormatter = logging.Formatter(defaultFormat)
        self.defHandler = logging.StreamHandler()
        self.defHandler.setFormatter(self.defaultFormatter)
        self.defHandler.setLevel(level.value)
        self.logger.addHandler(self.defHandler)
        self.SetOutLevel(level)

        blankFormat = '%(message)s'
        self.blankFormatter = logging.Formatter(blankFormat)
        self.txtHandler = None

    def Debug(self, content:str):
        self.logger.debug(content)
    
    def Info(self, content:str):
        self.logger.info(content)

    def Warning(self, content:str):
        self.logger.warning(content)

    def Error(self, content:str):
        self.logger.error(content)

    def print(self, content:str):
        if not self.defHandler:
            return
        self.defHandler.setFormatter(self.blankFormatter)
        self.logger.error(content)
        self.defHandler.setFormatter(self.defaultFormatter)

    def SetOutLevel(self, level:LogLevel):
        self.outLevel = level
        self.logger.setLevel(level.value)

    def AddTxtLogHandler(self, logFile:str):
        if self.txtHandler:
            return
        self.txtHandler = logging.FileHandler(logFile)
        self.txtHandler.setFormatter(self.defaultFormatter)
        self.txtHandler.setLevel(self.outLevel.value)
        self.logger.addHandler(self.txtHandler)
    
    def RemoveTxtLogHandler(self):
        if self.txtHandler:
            self.logger.removeHandler(self.txtHandler)
            self.txtHandler = None

    def RemoveDefLogHandler(self):
        if self.defHandler:
            self.logger.removeHandler(self.defHandler)
            self.defHandler = None